package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthExpertPowerMapService;
import com.orange.modules.entity.AuthExpertPowerMap;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/expertPower")
public class AuthExpertPowerMapController extends BaseController{
	
	private static final Logger logger = Logger.getLogger(AuthExpertPowerMapController.class);
	
	@Autowired
	private AuthExpertPowerMapService authExpertPowerMapService;
	
	/**
	 * 新增专家权限
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertExpertPowerMap" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertExpertPowerMap(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String expertId = map.get("expertId");
			String sid = map.get("sid");
			String firstType = map.get("firstType");  //一级指标（用逗号隔开）
			String secondType = map.get("secondType");  //必测项目（用逗号隔开）
			String thirdType = map.get("thirdType");  //选测项目（用逗号隔开）
			String forthType = map.get("forthType");  //其他项目（用逗号隔开）
			if(sid==null || StringUtils.isBlank(sid))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校ID不能为空!");
			if(expertId==null || StringUtils.isBlank(expertId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专家ID不能为空!");
			if(firstType!=null && StringUtils.isNotBlank(firstType)){
				String[] firstTypeList = firstType.split(",");
				for(int i = 0;i<firstTypeList.length;i++){
					AuthExpertPowerMap aepm = new AuthExpertPowerMap();
					aepm.setCreateTime(sdf.format(new Date()));
					aepm.setExpertId(expertId);
					aepm.setPowerType("10");  //一级指标类型是10
					aepm.setPowerValue(firstTypeList[i]);
					aepm.setSid(sid);
					authExpertPowerMapService.insertAuthExpertPowerMap(aepm);
				}
			}
			if(secondType!=null && StringUtils.isNotBlank(secondType)){
				String[] secondTypeList = secondType.split(",");
				for(int i = 0;i<secondTypeList.length;i++){
					AuthExpertPowerMap aepm = new AuthExpertPowerMap();
					aepm.setCreateTime(sdf.format(new Date()));
					aepm.setExpertId(expertId);
					aepm.setPowerType("20");  //必测项目类型是20
					aepm.setPowerValue(secondTypeList[i]);
					aepm.setSid(sid);
					authExpertPowerMapService.insertAuthExpertPowerMap(aepm);
				}
			}
			if(thirdType!=null && StringUtils.isNotBlank(thirdType)){
				String[] thirdTypeList = thirdType.split(",");
				for(int i = 0;i<thirdTypeList.length;i++){
					AuthExpertPowerMap aepm = new AuthExpertPowerMap();
					aepm.setCreateTime(sdf.format(new Date()));
					aepm.setExpertId(expertId);
					aepm.setPowerType("30");  //选测项目类型是30
					aepm.setPowerValue(thirdTypeList[i]);
					aepm.setSid(sid);
					authExpertPowerMapService.insertAuthExpertPowerMap(aepm);
				}
			}
			if(forthType!=null && StringUtils.isNotBlank(forthType)){
				String[] forthTypeList = forthType.split(",");
				for(int i = 0;i<forthTypeList.length;i++){
					AuthExpertPowerMap aepm = new AuthExpertPowerMap();
					aepm.setCreateTime(sdf.format(new Date()));
					aepm.setExpertId(expertId);
					aepm.setPowerType("40");  //其他项目类型是40
					aepm.setPowerValue(forthTypeList[i]);
					aepm.setSid(sid);
					authExpertPowerMapService.insertAuthExpertPowerMap(aepm);
				}
			}
			return toResponse(null,ReturnCode.SUCCESS,"新增权限成功");
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e);
			return toResponse(null,ReturnCode.FAIL,"新增权限失败");
		}
	}
	
	
	/**
	 * 查询该专家指定的考察项目
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryExpertPowerMapByExpertId" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryExpertPowerMapByExpertId(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try {
			String powerType = map.get("powerType"); //专家类型
			String expertId = map.get("expertId");   //专家编号
			if(expertId==null || StringUtils.isBlank(expertId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"专家编号不能为空");
			List<AuthExpertPowerMap> list = authExpertPowerMapService.queryOnceByExpertId(powerType, expertId);
			return toResponse(list,ReturnCode.SUCCESS,"查询成功");
		} catch (Exception e) {
			// TODO: handle exception
			logger.error(e);
			return toResponse(null,ReturnCode.SUCCESS,"查询失败");
		}
		
		
	}
	
}
